/* Akshit Gupta 2010012 DSA HWK-2 implementation of the hash funct: h=((ak mod m)+b)*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/********************************************************/
FILE *fp ;
int a[10][2];
struct stud
{
            char name[40] ;
            int rno ;
            
};
struct stud e[100];
/********************************************************/
main( )
{
int pwd,count1=0,d,u,l,o=0,r,x,q=0,t,w,sum=0,Index,pos;
int y=0,flag=0,k1=0,pos1,sum1=0,flag3=0;
char id[40],m[40];
char rep[40];     
int j=0,k=0,option;
char we=1;
int count=0,coun1=0,z=0;
char c;
char ch;
int retval;
/*******************************************************/
do
{
printf("1.Enter the new users\n2.Read the database\n3.Exit\n");
scanf("%d",&option);
switch(option)
{
case 1:
{     fp=fopen("ak.txt","a");
     if (fp==NULL)
{
            puts ( "Cannot open file" );
            exit(0);
}
     while (we==1)
{
            printf ( "\nEnter name");
          scanf ( "%s", e[k].name);
/*****************************************************************************************/
/*while((retval = fscanf(fp,"%s %d\n",&e[count1].name,&e[count1].rno)) != EOF )
{if(strcmp(rep,e[count1].name)==0)
{
flag++;
}	
count1++;	
}
if(flag==0)
{
strcmp(e[k].name,rep);
}
else
{printf("Userid exits\n");
} */
/*******************************************************************************************/
printf ("\nEnter Password");
scanf ("%d", &e[k].rno);
fprintf ( fp, "%s %d \n", e[k].name, e[k].rno ) ;
++k;                    
printf ( "Add another record (1=yes) " ) ;
scanf("%d",&we);
}
/*
while(j<=k)
{
printf("%s\n",e[j].name);
j++;
}*/
fclose (fp);
}
break;
/*******************************************************************/
case 2:
          fp=fopen("ak.txt","r");
     if(fp==NULL)
{
            puts ("Cannot open file");
            exit(0);
}
while ((retval = fscanf(fp,"%s %d\n",&e[count].name,&e[count].rno)) != EOF )
count++;	
/****************************************/ 
/*while(z<=count)                            
{
printf("%s\n",e[z].name);
z++;
}
*/
fclose (fp);
/***********************************************/
for(x=0;x<count;x++)
{
sum=0;
for(Index=0;Index<strlen(e[x].name);Index++)
{
sum+=(e[x].name[Index]);                                   /*Adds (the ASCII values of) the String characters*/
} 
pos=((2*sum)%10)+1;                                                /*Hash Fuction*/
/****************************************/
while(a[pos][0]!=NULL)
{
                   pos+=1;
}      
a[pos][0]=e[x].rno;
a[pos][1]=e[x].name;
}
/****************************************/
printf("Enter the username\n");
scanf("%s",id);
while(y<=count)
{
if(strcmp(id,e[y].name)==0)
{
flag3++;
break;
}
++y;                
}
if(flag3==0)
{
printf("\n\nInvalid user id\n\n");
}
else
{
for(l=0;l<strlen(id);l++)
{
sum1+=(id[l]);                        /*Adds (the ASCII values of) the String characters*/
} 
pos=((2*sum)%10)+1;                                                /*Hash Fuction*/
while(coun1<3)
{
printf("Enter the paasword\n");
scanf("%d",&pwd);
if(a[pos1][0]==pwd)
{
printf("Welcome\n");
break;
}
++coun1;
}
if(coun1==3)
{
printf("\n\nSorry you failed in all 3 attempts\n\n\n");
}}
break;
/**************************************************************/
case 3:
     printf("\n");
break;

default:printf("Wrong choice entered\n\n");
}
}while(option!=3);
}                          /*End of the program*/
